package com.yex.service.modify.order;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yex.common.exception.RRException;
import com.yex.dao.UserDao;
import com.yex.dao.UserTransferLogDao;
import com.yex.entity.UserEntity;
import com.yex.entity.UserTransferLogEntity;
import com.yex.service.modify.BaseService;
import com.yex.service.modify.WalletService;
import com.yex.util.CommonModifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserPriceService extends BaseService<UserDao, UserEntity> {

    @Autowired
    private WalletService walletService;

    @Autowired
    private UserTransferLogDao userTransferLogDao;

    public UserEntity findById(Integer userId) {
        UserEntity userEntity = baseMapper.selectById(userId);
        if (userEntity == null) throw new RRException("找不到指定的用户信息");
        if (userEntity.getStatus().compareTo(1) != 0) throw new RRException("该用户已删除或被禁用");
        return userEntity;
    }

    public void frozen(Integer userId, BigDecimal frozen) {
        frozen = frozen.setScale(2, BigDecimal.ROUND_HALF_UP);
        UserEntity userEntity = findById(userId);
        if (userEntity.getTotal().compareTo(frozen) < 0) throw new RRException("用户余额不足");
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setFrozen(frozen);
        userEntity1.setId(userEntity.getId());
        Integer count = baseMapper.updateFrozen(userEntity1);
        if (count > 1) throw new RRException("更新余额失败");
    }

    public void unfrozen(Integer userId, BigDecimal frozen) {
        frozen = frozen.setScale(2, BigDecimal.ROUND_HALF_UP);
        UserEntity userEntity = findById(userId);
        if (userEntity.getFrozen().compareTo(frozen) < 0) frozen = userEntity.getFrozen();
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setFrozen(frozen);
        userEntity1.setId(userEntity.getId());
        Integer count = baseMapper.updateUnFrozen(userEntity1);
        if (count > 1) throw new RRException("更新余额失败");
    }

    public void transferUser(Integer userId, Integer transferId, BigDecimal frozen) {
        //  减少user的冻结数量
        UserEntity userEntity = findById(userId);
        if (userEntity.getFrozen().compareTo(frozen) < 0) frozen = userEntity.getFrozen();
        userEntity.setFrozen(frozen);
        baseMapper.updateReduceFrozen(userEntity);

        userEntity = findById(transferId);
        userEntity.setTotal(frozen);
        baseMapper.updateAdd(userEntity);
    }

    public void balance2Coin(BigDecimal balance) {
        UserEntity userEntity = validateLoginUser(CommonModifyUtil.getRequest());
        balance = balance.setScale(2, BigDecimal.ROUND_HALF_UP);
        if (userEntity.getTotal().subtract(balance).compareTo(BigDecimal.ZERO) < 0) throw new RRException("用户余额不足");
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setId(userEntity.getId());
        userEntity1.setTotal(balance);
        Integer count = baseMapper.updateReduce(userEntity1);
        UserTransferLogEntity userTransferLogEntity = new UserTransferLogEntity();
        userTransferLogEntity.setCreateTime(new Date());
        userTransferLogEntity.setUserId(userEntity.getId());
        userTransferLogEntity.setNumber(balance);
        userTransferLogDao.insert(userTransferLogEntity);
        if (count > 1) throw new RRException("转账失败");
        walletService.updateAdd(userEntity.getId(), balance);
    }


    public List<UserTransferLogEntity> transferLogByPage(Integer page, Integer rows) {
        UserEntity userEntity = validateLoginUser(CommonModifyUtil.getRequest());
        Page<UserTransferLogEntity> pageEntity = new Page<>(page, rows);
        return userTransferLogDao.selectByPage(pageEntity, userEntity.getId());
    }


}
